package com.community.controller;

import com.alibaba.fastjson.JSONObject;
import com.community.constant.JwtConstants;
import com.community.constant.TokenConstants;
import com.community.domain.enity.UserVo;
import com.community.domain.enity.vo.SendSmsVo;
import com.community.domain.handler.UserLogger;
import com.community.domain.response.JwtResponse;
import com.community.result.Result;
import com.community.service.UserLoginService;
import com.community.utils.JwtUtils;
import com.community.utils.StringUtils;
import com.xiaoleilu.hutool.Hutool;
import com.xiaoleilu.hutool.util.RandomUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 类名称： UserLoginController
 * 描述：
 * 创建人：zyj
 * 创建时间：2023/11/7 12:48
 */
@RestController
@RequestMapping("/user")
@Log4j2
public class UserLoginController {

    @Autowired
    UserLoginService userLoginService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @UserLogger(operation = "短信注册")
    @PostMapping("/sendSms/{phone}")
    public Result<String> sendSms(@PathVariable String phone){

        if(StringUtils.isEmpty(phone)){
            return Result.error("手机号不能为空");
        }
        SendSmsVo sendSmsVo = new SendSmsVo();
        String msgId = RandomUtil.randomNumbers(6);
        sendSmsVo.setMsgId(msgId);

        userLoginService.sendSms(sendSmsVo);

        return Result.success();
    }

    /**
     * 账号密码登录
     * @param userVo
     * @return
     */
    @UserLogger(operation = "账号密码登录")
    @PostMapping("/loginByPassword")
    public Result<JwtResponse> loginByPassword(@RequestBody UserVo userVo){

        Result<JwtResponse> result = null;

        // 验证
        if(StringUtils.isEmpty(userVo.getUsername()) ||StringUtils.isEmpty(userVo.getPassword())){
            result  = Result.error("账号密码不能为空");
        } else {
            result = userLoginService.login(userVo);
        }
        return result;

    }

    /**
     * 登出
     */
    @PostMapping("/logout")
    public Result logout(){
        //获取token
        String token = request.getHeader(TokenConstants.TOKEN);
        //解析token
        String userKey = JwtUtils.getUserKey(token);
        redisTemplate.delete(TokenConstants.LOGIN_TOKEN_KEY+userKey);
        return Result.success();
    }

    /**
     * 获取数据
     * @return
     */
    @GetMapping("/info")
    public Result<UserVo> info(){
        //获取token
        String token = request.getHeader(TokenConstants.TOKEN);
        //解析token
        String userKey = JwtUtils.getUserKey(token);
        //获取用户数据
        String userJson = String.valueOf(redisTemplate.opsForValue().get(JwtConstants.SECRET + userKey));
        //反序列化
        UserVo user = JSONObject.parseObject(userJson, UserVo.class);

        return Result.success(user);
    }


}
